package org.theonept.travian.t4a.village;

import java.util.List;

import org.theonept.travian.model.Compte;
import org.theonept.travian.model.Village;
import org.theonept.travian.t4a.AbstractFragment;
import org.theonept.travian.t4a.R;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * A dummy fragment representing a section of the app, but that simply displays
 * dummy text.
 */
public class VillageSectionFragment extends AbstractFragment implements
		AdapterView.OnItemClickListener {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */

	private ListView messageListView;
	private ListVillageAdapter listVillageAdapter;
	private Compte compte;

	public VillageSectionFragment() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_compte_message,
				container, false);
		messageListView = (ListView) rootView.findViewById(R.id.listMessage);
		if (listVillageAdapter == null) {
			listVillageAdapter = new ListVillageAdapter(getActivity());
		}
		this.compte = (Compte) getArguments().getSerializable("compte");
		List<Village> villages = (List<Village>) getArguments()
				.getSerializable("villages");
		if (villages != null) {
			setData(villages);
		}
		messageListView.setAdapter(listVillageAdapter);
		messageListView.setOnItemClickListener(this);
		return rootView;
	}

	public void setData(List<Village> villages) {
		listVillageAdapter.setData(villages);
		listVillageAdapter.clear();
		listVillageAdapter.addAll(villages);

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {
		Village village = listVillageAdapter.getItem(position);
		Intent intentMessage = new Intent(getActivity(), VillageActivity.class);
		intentMessage.putExtra("village", village);
		intentMessage.putExtra("compte", compte);
		startActivity(intentMessage);
	}
}